package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.client.enums.BalanceLogTypeEnum;
import com.meida.module.pay.client.entity.PayBalanceLog;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 后台用户余额日志列表
 */
@Component
public class UserBalanceLogPageListHandler implements PageInterceptor<PayBalanceLog> {


    @Override
    public void prepare(CriteriaQuery<PayBalanceLog> cq, PageParams pageParams, EntityMap params) {
        String queryDate = params.get("queryDate");
        cq.eq("DATE_FORMAT(pbl.createTime,'%Y-%m-%d')", queryDate);
        cq.like(AppUser.class, "nickName");
        cq.like(AppUser.class, "mobile");
        cq.like(AppUser.class, "userNo");
        cq.select(AppUser.class, "nickName", "mobile", "userNo");
        cq.createJoin(AppUser.class).setMainField("userId").setJoinField("userId");
    }

    @Override
    public void complete(CriteriaQuery<PayBalanceLog> cq, List<EntityMap> result, EntityMap extra) {
        result.forEach(entityMap -> {
            Integer logType = entityMap.getInt("logType");
            BalanceLogTypeEnum balanceLogTypeEnum = BalanceLogTypeEnum.getByValue(logType);
            String logTypeText = "";
            if (FlymeUtils.isNotEmpty(balanceLogTypeEnum)) {
                logTypeText = balanceLogTypeEnum.getLabel();
            }
            entityMap.put("logTypeText", logTypeText);
        });
    }
}
